package gpx;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


/**
 * Class representing Main Dialog of Application.
 * @author Josef Vermach jr. home
 *
 */
public class GpxConvertDialog extends JFrame {
	private static final long serialVersionUID = 1L;
	/**
	 * Text Area which is console of Application
	 */
	TextArea textAreaConsole; 
	/**
	 * Default Language setting of App.
	 */
	String language = loadLanguage(); //Default language of Application
	/**
	 * Overriden implicit constructor of Application
	 */
	/**
	 * File which program will work with
	 */
	File file;
	private boolean isFileOpened;
	public GpxConvertDialog () {
		this.setLayout(new BorderLayout(10,10)); //drawing of layout
		this.setVisible(false);  
		textAreaConsole = new TextArea(); //
		textAreaConsole.setEditable(false); //Setting text area not for editing
		textAreaConsole.setCursor(null); //turning off cursor
		this.setTitle(Localization.getString(language,"BTC"));
		this.add(textAreaConsole,"Center");
		textAreaConsole.append(Localization.getString(language, "WELCOME"));
		
		MenuBar mb = new MenuBar(); //Creating of menu.
		this.setMenuBar(mb);
		Menu mFile = new Menu(Localization.getString(language, "FILE"));
		mb.add(mFile);
		MenuItem mIOpen = new MenuItem(Localization.getString(language, "OPEN"), new MenuShortcut(KeyEvent.VK_O));
		mFile.add(mIOpen);
		mIOpen.addActionListener(new ActionListener() { //open file
			@Override
			public void actionPerformed(ActionEvent e) {
				 open();
			}
		});
		
		MenuItem mIRestart = new MenuItem(Localization.getString(language, "RESTART")); //restart app
		mFile.add(mIRestart);
		mIRestart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 restart();
			}
		});
		MenuItem mIClose = new MenuItem(Localization.getString(language, "CLOSE")); //close app
		mFile.add(mIClose);
		mIClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				 close();
			}
		});
//Menu Edit
		Menu mEdit = new Menu (Localization.getString(language, "EDIT"));
		mb.add(mEdit);
		MenuItem mIView = new MenuItem(Localization.getString(language, "VIEW"));
		mEdit.add(mIView);
//		mIView.setEnabled(false);
		mIView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				view();
			}
		});
		
//Menu Options		
		Menu mOptions = new Menu(Localization.getString(language, "OPTIONS")); //Options menu
		mb.add(mOptions);
		MenuItem mIShowOptions = new MenuItem(Localization.getString(language, "SHOW"));
		mOptions.add(mIShowOptions);
		Menu mLanguageOptions = new Menu(Localization.getString(language, "LANGUAGES"));
		mOptions.add(mLanguageOptions);
		final CheckboxMenuItem mIEnglish = new CheckboxMenuItem(Localization.getString(language, "ENGLISH"));
		final CheckboxMenuItem mICzech = new CheckboxMenuItem(Localization.getString(language, "CZECH"));
		if (this.loadLanguage().endsWith("EN")) {
			mIEnglish.setState(true);
		}
		else if (this.loadLanguage().endsWith("CZ")) {
			mICzech.setState(true);
		}
//		else if we have another language
		mLanguageOptions.add(mIEnglish);
		mLanguageOptions.add(mICzech);
		mICzech.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				mICzech.setState(true);
				mIEnglish.setState(false);
				JOptionPane.showMessageDialog(rootPane, Localization.getString(language, "MESSAGES.NeedRestart"));
				language = "messages_cs_CZ";
			}
		});
		mIEnglish.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				mICzech.setState(false);
				mIEnglish.setState(true);
				JOptionPane.showMessageDialog(rootPane, Localization.getString(language, "MESSAGES.NeedRestart"));
				language = "messages_en_EN";
			}
		});
		mIShowOptions.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				textAreaConsole.append(Localization.getString(language, "MESSAGES.ShowingOptions"));
				new Options();
			}
		});
		Menu mAbout = new Menu(Localization.getString(language, "ABOUT"));
		mb.add(mAbout);
		MenuItem mIAbout = new MenuItem(Localization.getString(language, "ABOUTBTC"));
		mIAbout.setEnabled(false);
		MenuItem mIHelp = new MenuItem(Localization.getString(language, "HELP"));
		mIHelp.setEnabled(false);
		MenuItem mIContact = new MenuItem(Localization.getString(language, "CONTACT"));
		mAbout.add(mIContact);
		mIContact.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Log: Contacting");
				Sender sm = new Sender();
				sm.doSend();
				
			}
		});
		mAbout.add(mIAbout);
		mAbout.add(mIHelp);
		this.pack();
		this.setVisible(true);
	}

	/**
	 * Method opens a file for convertion and call GpxConvertor()
    */
	public void open() {
		this.textAreaConsole.append(Localization.getString(language, "MESSAGES.OpeningFile"));
		String filename = File.separator+"tmp";
		Boolean first = true;
		File selFile = new File("file.txt");
		do {
		if (!first) {
			this.textAreaConsole.append(selFile.getName()+Localization.getString(language, "MESSAGES.NoGpx"));
			this.textAreaConsole.append(Localization.getString(language, "MESSAGES.OpeningFile"));
		}
		first = false;
		JFileChooser fc = new JFileChooser(new File(filename));
		fc.addChoosableFileFilter(new MyFilter());
		fc.showOpenDialog(new JFrame());
		fc.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//JFileChooser chooser = (JFileChooser)e.getSource();
				if (JFileChooser.CANCEL_SELECTION.equals(e.getActionCommand())){
					System.out.println(Localization.getString(language, "MESSAGES.OpeningFileCanceled"));
				}
			}
		});
		selFile = fc.getSelectedFile();
		if (selFile==null){
			textAreaConsole.append(Localization.getString(language, "MESSAGES.OpeningFileCanceled"));
			break;
		}
//		else {
//			//this.textAreaConsole.append(selFile.getName()+"\n");
//		}
		} while (!selFile.getName().endsWith(".gpx"));
		if (selFile==null) {
			this.textAreaConsole.append(Localization.getString(language, "MESSAGES.NoFile"));
		}
		else {
			this.file = selFile;
			this.isFileOpened = true;
			this.textAreaConsole.append(Localization.getString(language, "MESSAGES.FileOpened")+selFile.getName()+"\n");
			this.textAreaConsole.append(Localization.getString(language, "MESSAGES.FileOpenedWhatToDo"));
//			GpxConvertor gc = new GpxConvertor(selFile);
//			gc.prinfFileContent();
//			gc.saveAs();
		}
	}
	
	/**
	 * Method closes Application (and call method for saving settings and languages).
	 */
	public void close() {
		this.saveLanguage();
		this.saveSettings();
		this.dispose();
		System.exit(0);
	}
	
	/**
	 * Method saves all Application's settings.
	 */
	private void saveSettings() {
	}

	/**
	 * Method finishes Application with current settings and start another instance of Application and load previously saved settings
	 */
	public void restart() {
		this.saveLanguage();
		this.setVisible(false);
		this.dispose();
		new GpxConvertDialog();
		
	}
	
	/**
	 * Method load language settins which is saved in text file language.txt
	 * @return name of properties file with localization
	 */
	public String loadLanguage() {
		String line = null;;
		File file = new File("language.txt");
		try {
			BufferedReader in = new BufferedReader (new FileReader(file));
			line = in.readLine();
			in.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return line;
	}

	/**
	 * Method saves current language settings into language.txt file.
	 */
	public void saveLanguage() {
		File file = new File ("language.txt");
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(file));
			out.write(this.language);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * Method views an entire content of the file and allows to change it. 
	 */
	public void view() {
		if (isFileOpened) {
			FileViewer fileViewer= new FileViewer(this.file, language);
			fileViewer.view();
			fileViewer.saveFile();
		}
		else {
			JOptionPane.showMessageDialog(rootPane, Localization.getString(language, "MESSAGES.NoFile"));
		}
	}
}
